/**
 * 
 */
package com.itboye.mengwu.app.ui.main;

import java.util.Observer;

import android.util.Log;

import com.itboye.core.interfaces.ICompleteListener;
import com.itboye.core.object.BasePresenter;
import com.itboye.core.object.ResultEntity;
import com.itboye.core.utils.DebugLog;
import com.itboye.mengwu.data.interfaces.IUserRepository;
import com.itboye.mengwu.data.model.PersonDataBean;
import com.itboye.mengwu.data.model.UserModel;
import com.itboye.mengwu.data.repository.UserRepository;

/**
 * @author hebidu
 * 
 */
public class MainPresenter extends BasePresenter {

	//登录
	public static final String LoginSuccessEvent = MainPresenter.class
			.getName() + "_Login_success";
	public static final String LoginFailureEvent = MainPresenter.class
			.getName() + "_Login_failure";
	
	//登录
		public static final String PayNowSuccess = MainPresenter.class
				.getName() + "_PayNowSuccess";
		public static final String PayNowFail = MainPresenter.class
				.getName() + "_PayNowFail";
	
	//注册
	public static final String RegisterSuccessEvent = MainPresenter.class
			.getName() + "_Register_success";
	public static final String RegisterFailureEvent = MainPresenter.class
			.getName() + "_Register_failure";
	
	//修改密码
	public static final String UpdatePwdByPhoneFailureEvent = MainPresenter.class
			.getName() + "_UPdatePwd_failure";
	public static final String UpdatePwdByPhoneSuccessEvent = MainPresenter.class
			.getName() + "_UpdatePwd_success";
	
	//更新个人资料
	public static final String UpdateUserInfoSuccessEvent=MainPresenter.class
			.getName() + "_UpdateUsrInfo_success";
	public static final String UpdateUserInfoFailureEvent=MainPresenter.class
			.getName() + "_UpdateUsrInfo_fail";
	
	//绑定手机号
	public static final String BindPhoneNumberFailureEvent=MainPresenter.class
			.getName() + "_BindPhoneNumber_fail";
	public static final String BindPhoneNumberSuccessEvent=MainPresenter.class
			.getName() + "_BindPhoneNumber_success";
	
	
	//通過密碼修改密碼
	public static final String UpdatePswByPwdFailureEvent=MainPresenter.class
			.getName() + "_Updatepsw_fail";
	public static final String UpdatePswByPwdSuccessEvent=MainPresenter.class
			.getName() + "_Updatepsw_success";
	
	
	//修改绑定手机号
	public static final String UpdateBindPhoneFailureEvent=MainPresenter.class
			.getName() + "_UpdateBindPhone_fail";
	public static final String UpdateBindPhoneSuccessEvent=MainPresenter.class
			.getName() + "_UpdateBindPhone_success";
	
	public static final String WxPaySuccess=MainPresenter.class
			.getName() + "_WxPay_success";
	public static final String WxPayFail=MainPresenter.class
			.getName() + "_WxPay_fail";
	
	public static final String testSuccess=MainPresenter.class
			.getName() + "_test_success";
	public static final String testFail=MainPresenter.class
			.getName() + "_test_fail";
	
	
	public MainPresenter(Observer observer) {
		super(observer);
	}

	public void payNow(String uid,String orderCode,String payType,String payMoney) {
		IUserRepository<PersonDataBean> repo = new UserRepository(
				new ICompleteListener() {

					@Override
					public void success(ResultEntity result) {
						Log.v("MainPresenter", "login success");
						result.setEventTag(Tag_Success);
						result.setEventType(PayNowSuccess);
						MainPresenter.this.setChanged();
						MainPresenter.this.notifyObservers(result);
					}

					@Override
					public void failure(ResultEntity result) {
						Log.v("MainPresenter", "login failure");
						result.setEventTag(Tag_Error);
						result.setEventType(PayNowFail);
						MainPresenter.this.setChanged();
						MainPresenter.this.notifyObservers(result);
					}
				});

		
		repo.payNow(uid, orderCode, payType, payMoney);
	}
	
	public void login(String phoneNumber, String pwd,int type) {
		IUserRepository<PersonDataBean> repo = new UserRepository(
				new ICompleteListener() {

					@Override
					public void success(ResultEntity result) {
						Log.v("MainPresenter", "login success");
						result.setEventTag(Tag_Success);
						result.setEventType(LoginSuccessEvent);
						MainPresenter.this.setChanged();
						MainPresenter.this.notifyObservers(result);
					}

					@Override
					public void failure(ResultEntity result) {
						Log.v("MainPresenter", "login failure");
						result.setEventTag(Tag_Error);
						result.setEventType(LoginFailureEvent);
						MainPresenter.this.setChanged();
						MainPresenter.this.notifyObservers(result);
					}
				});

		PersonDataBean model = new PersonDataBean();
		model.setUsername(phoneNumber);
		model.setPassword(pwd);
		model.setType(type);
		repo.login(model);
	}

	public void register(String phonNumber, String password, String yanzhengma) {

		DebugLog.v("MainPresenter", "register start");

		IUserRepository<PersonDataBean> repo = new UserRepository(
				new ICompleteListener() {

					@Override
					public void success(ResultEntity result) {
						Log.v("MainPresenter", "register success");
						result.setEventTag(Tag_Success);
						result.setEventType(RegisterSuccessEvent);
						MainPresenter.this.setChanged();
						MainPresenter.this.notifyObservers(result);
					}

					@Override
					public void failure(ResultEntity result) {
						Log.v("MainPresenter", "register failure");
						result.setEventTag(Tag_Error);
						result.setEventType(RegisterFailureEvent);
						MainPresenter.this.setChanged();
						MainPresenter.this.notifyObservers(result);
					}
				});

		PersonDataBean model = new PersonDataBean();
		model.setUsername(phonNumber);
		model.setPassword(password);
		model.setYanzhengma(yanzhengma);
		repo.register(model);
	}

	public void updatePasswordByTelephone(String code, String pwd,
			String phoneNumber) {
		IUserRepository<PersonDataBean> repo = new UserRepository(
				new ICompleteListener() {

					@Override
					public void success(ResultEntity result) {
						Log.v("MainPresenter", "register success");
						result.setEventTag(Tag_Success);
						result.setEventType(UpdatePwdByPhoneSuccessEvent);
						MainPresenter.this.setChanged();
						MainPresenter.this.notifyObservers(result);
					}

					@Override
					public void failure(ResultEntity result) {
						Log.v("MainPresenter", "register failure");
						result.setEventTag(Tag_Error);
						result.setEventType(UpdatePwdByPhoneFailureEvent);
						MainPresenter.this.setChanged();
						MainPresenter.this.notifyObservers(result);
					}
				});

		PersonDataBean model = new PersonDataBean();
		model.setUsername(phoneNumber);
		model.setPassword(pwd);
		model.setYanzhengma(code);
		model.setType(1);
		repo.update(model);
	}
	
//	public void updatePasswordByPwd(String uid,String code, String pwd,
//			String phoneNumber) {
//		IUserRepository<PersonDataBean> repo = new UserRepository(
//				new ICompleteListener() {
//
//					@Override
//					public void success(ResultEntity result) {
//						Log.v("MainPresenter", "register success");
//						result.setEventTag(Tag_Success);
//						result.setEventType(UpdatePwdSuccessEvent);
//						MainPresenter.this.setChanged();
//						MainPresenter.this.notifyObservers(result);
//					}
//
//					@Override
//					public void failure(ResultEntity result) {
//						Log.v("MainPresenter", "register failure");
//						result.setEventTag(Tag_Error);
//						result.setEventType(UPdatePwdFailureEvent);
//						MainPresenter.this.setChanged();
//						MainPresenter.this.notifyObservers(result);
//					}
//				});
//
//		PersonDataBean model = new PersonDataBean();
//		model.setUsername(phoneNumber);
//		model.setPassword(pwd);
//		model.setYanzhengma(code);
//		repo.update(model);
//	}
	
	public void updatePasswordByPwd(String uid, String newPwd,String pwd,
			String phoneNumber) {
		IUserRepository<PersonDataBean> repo = new UserRepository(
				new ICompleteListener() {

					@Override
					public void success(ResultEntity result) {
						Log.v("MainPresenter", "register success");
						result.setEventTag(Tag_Success);
						result.setEventType(UpdatePwdByPhoneSuccessEvent);
						MainPresenter.this.setChanged();
						MainPresenter.this.notifyObservers(result);
					}

					@Override
					public void failure(ResultEntity result) {
						Log.v("MainPresenter", "register failure");
						result.setEventTag(Tag_Error);
						result.setEventType(UpdatePwdByPhoneFailureEvent);
						MainPresenter.this.setChanged();
						MainPresenter.this.notifyObservers(result);
					}
				});

		PersonDataBean model = new PersonDataBean();
		model.setUid(uid);
		model.setType(5);
		model.setPassword(pwd);
		model.setNewPwd(newPwd);
		repo.update(model);
	}
	
	public void updateUserInfoByUid(PersonDataBean model) {
		/**
		 * uid是用户IDmobile否用户手机realname否真实姓名email否邮箱birthday否生nickname否昵称sex否性别(0:女,1:男 , 其它值:未知)qq否qq号head		 */
		IUserRepository<PersonDataBean> repo = new UserRepository(
				new ICompleteListener() {

					@Override
					public void success(ResultEntity result) {
						Log.v("MainPresenter", "register success");
						result.setEventTag(Tag_Success);
						result.setEventType(UpdateUserInfoSuccessEvent);
						MainPresenter.this.setChanged();
						MainPresenter.this.notifyObservers(result);
					}

					@Override
					public void failure(ResultEntity result) {
						Log.v("MainPresenter", "register failure");
						result.setEventTag(Tag_Error);
						result.setEventType(UpdateUserInfoFailureEvent);
						MainPresenter.this.setChanged();
						MainPresenter.this.notifyObservers(result);
					}
				});
		repo.update(model);
	}
	
	public void bindPhoneNumber(PersonDataBean model) {
		/**
		 * uid是用户IDmobile否用户手机realname否真实姓名email否邮箱birthday否生nickname否昵称sex否性别(0:女,1:男 , 其它值:未知)qq否qq号head		 */
		IUserRepository<PersonDataBean> repo = new UserRepository(
				new ICompleteListener() {

					@Override
					public void success(ResultEntity result) {
						Log.v("MainPresenter", "register success");
						result.setEventTag(Tag_Success);
						result.setEventType(BindPhoneNumberFailureEvent);
						MainPresenter.this.setChanged();
						MainPresenter.this.notifyObservers(result);
					}

					@Override
					public void failure(ResultEntity result) {
						Log.v("MainPresenter", "register failure");
						result.setEventTag(Tag_Error);
						result.setEventType(BindPhoneNumberSuccessEvent);
						MainPresenter.this.setChanged();
						MainPresenter.this.notifyObservers(result);
					}
				});

		repo.update(model);
	}
	
	public void updateBindPhoneNumber(PersonDataBean model) {
		/**
		 * 更换绑定手机号码
		 * */
		IUserRepository<PersonDataBean> repo = new UserRepository(
				new ICompleteListener() {

					@Override
					public void success(ResultEntity result) {
						Log.v("MainPresenter", "register success");
						result.setEventTag(Tag_Success);
						result.setEventType(UpdateBindPhoneSuccessEvent);
						MainPresenter.this.setChanged();
						MainPresenter.this.notifyObservers(result);
					}

					@Override
					public void failure(ResultEntity result) {
						Log.v("MainPresenter", "register failure");
						result.setEventTag(Tag_Error);
						result.setEventType(UpdateBindPhoneFailureEvent);
						MainPresenter.this.setChanged();
						MainPresenter.this.notifyObservers(result);
					}
				});

		repo.update(model);
	}
	public void wxPay(String orderCode, String totalPrice, String desc) {
		// TODO Auto-generated method stub
		IUserRepository<PersonDataBean> repo = new UserRepository(
				new ICompleteListener() {

					@Override
					public void success(ResultEntity result) {
						Log.v("MainPresenter", "register success");
						result.setEventTag(Tag_Success);
						result.setEventType(WxPaySuccess);
						MainPresenter.this.setChanged();
						MainPresenter.this.notifyObservers(result);
					}

					@Override
					public void failure(ResultEntity result) {
						Log.v("MainPresenter", "register failure");
						result.setEventTag(Tag_Error);
						result.setEventType(WxPayFail);
						MainPresenter.this.setChanged();
						MainPresenter.this.notifyObservers(result);
					}
				});

		repo.wxPay(orderCode, totalPrice, desc);
	}
	
	public void testPay(String uid,String code){
		IUserRepository<PersonDataBean> repo = new UserRepository(
				new ICompleteListener() {

					@Override
					public void success(ResultEntity result) {
						Log.v("MainPresenter", "register success");
						result.setEventTag(Tag_Success);
						result.setEventType(testSuccess);
						MainPresenter.this.setChanged();
						MainPresenter.this.notifyObservers(result);
					}

					@Override
					public void failure(ResultEntity result) {
						Log.v("MainPresenter", "register failure");
						result.setEventTag(Tag_Error);
						result.setEventType(testFail);
						MainPresenter.this.setChanged();
						MainPresenter.this.notifyObservers(result);
					}
				});

		repo.testPay(uid,code);
	}
}
